﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Business.IManager;
using Entity;
using Business.Manager;
namespace OnlinePayment.OP
{
    public partial class RechargeNetworkView : System.Web.UI.Page
    {
        INetworkPortManager portManager = new NetworkPortManager();
        IOrderManager orderManager = new OrderManager();
        NetworkPort port = new NetworkPort();
        CampusNetworkHistoryOrder order = new CampusNetworkHistoryOrder();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["User"] == null)
                {
                    Response.Redirect("/Account/LogIn.aspx");
                }
                else
                {
                    Student stu = (Student)Session["User"];
                    lbl_stuId.Text = stu.StudentId;
                }
                NetWorkPortInforBind();
            }
        }

        protected void NetWorkPortInforBind()
        {
            port = portManager.getNetworkPortInform(lbl_stuId.Text.ToString());
            if (port != null)
            {
                if (port.Status.Equals("当前可用"))
                {
                    string time = port.AvaliableEndTime.ToString();
                    lblPortState.Text = port.Status;
                    lblAvailableEndTime.Text = lblAvailableEndTime.Text + ":" + time;
                    lblAvailableEndTime.Visible = true;
                }
            }
        }
        protected void btnRecharge_Click(object sender, EventArgs e)
        {
            int month = 0;
            //奇怪
            port = portManager.getNetworkPortInform(lbl_stuId.Text.ToString());
            if (port.Status.Equals("未开通") == false)
            {
                if (rbMonth.Checked)
                {
                    month = Convert.ToInt32(txtMonth.Text);
                    order.TotalAmount = month * 30;
                    order.PaymentType = month + "月网费";

                }
                else if (rbYear.Checked)
                {
                    month = Convert.ToInt32(txtYear.Text) * 12;
                    order.TotalAmount = Convert.ToInt32(txtYear.Text) * 270;
                    order.PaymentType = txtYear.Text + "年网费";
                }
                else
                {
                    month = 6;
                    order.TotalAmount = 180;
                    order.PaymentType = "半年网费";
                }
                order.OrderDate = System.DateTime.Now;
                port.AvailableLastMonth = port.AvailableLastMonth + month;
                port.Status = "当前可用";
                order.OrderId = orderManager.getOrderId(order.PaymentType);
                order.portId = port.PortId;
                Session["GoodsID"] = Convert.ToString(order.portId);
                Session["Order"] = order;
                Session["Port"] = port;
                Response.Redirect("OrderConfirmView.aspx");
            }
        }
    }
}